#coding=utf-8
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from zhaokelai.users.models import users
from functools import wraps

def skinFolder(request):
    skin = request.session.get('skin', None)
    if skin:
        return skin+"/"
    else:
        return ""
    
def checkLogin(view_func):
    def _checklogin(request, *args, **kwargs):            
        if request.method == 'POST':
            REFERER=request.META['HTTP_REFERER']
        else:
            REFERER=request.get_full_path()
        _user = request.session.get('user', None)
        if _user:
            return view_func(request, *args, **kwargs)
        else:
            return render_to_response('redirect.html', {'REFERER':REFERER})
    return wraps(view_func)(_checklogin)

def admin_authenticated(view_func):
    def _checklogin(request, *args, **kwargs):            
        if request.method == 'POST':
            REFERER=request.META['HTTP_REFERER']
        else:
            REFERER=request.get_full_path()
        _user = request.session.get('user', None)
        if _user:
            _iuser = users.objects.get(pk=int(_user))
            if _iuser and _iuser.permissions=='A':
                return view_func(request, *args, **kwargs)
            else:
                return render_to_response('permissions.html', {'message':'需要管理员身份','REFERER':request.META['HTTP_REFERER']})
        else:
            return render_to_response('redirect.html', {'REFERER':REFERER})
    return wraps(view_func)(_checklogin)